/**
 * nghilt
 * 2011/09/16
 *
 * Copyright by N&N.,Inc
 */
package vn.nnn.shogi.quanco;

import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;

import javax.swing.JApplet;

import at.com.vn.chess.util.RectangleUtils;

import vn.nnn.shogi.board.Board;

/**
 * 
 */
public class QuanTuongBac extends QuanCo {

	public QuanTuongBac( Rectangle toaDo, int color, boolean normalDirection, JApplet mainApplet ) {
		super( toaDo, color, normalDirection, mainApplet );	
	}
	
	@Override
	public void setToaDoNhaTu() {
		toaDoNhaTu = new Point( 1, 1 );
	}

	/* (non-Javadoc)
	 * @see vn.nnn.shogi.quanco.QuanCo#canMove(java.awt.Rectangle, vn.nnn.shogi.quanco.QuanCo, java.util.ArrayList)
	 */
	@Override
	public boolean canMove( Board board, Rectangle destination,
			QuanCo foundedPiece, ArrayList<QuanCo> listQuanCo ) {
		boolean canmove = false;
		
//		Rectangle quanThap;
//		Rectangle quanCao;
//		
////		if( color == QUAN_DEN ) {
//			quanThap = destination;
//			quanCao = toaDo;
////		}else {
////			quanThap = toaDo;
////			quanCao = destination;
////		}
			Rectangle quanThap = toaDo;
			Rectangle quanCao = destination;
			
			if( normalDirection ) {
				if( color == QUAN_DEN ) {
					quanThap = destination;
					quanCao = toaDo;
				}
			}else {
				if( color == QUAN_TRANG ) {
					quanThap = destination;
					quanCao = toaDo;
				}
			}
		
		// Neu la mot hang o phia tren
		if( RectangleUtils.isNextRow( quanThap, quanCao ) ) {
			
			// Neu cung cot hoac la 2 cot o ben canh
			if( RectangleUtils.isInSameColumn( quanThap, quanCao ) 
					|| RectangleUtils.isColumnBeside( quanThap, quanCao ) ) {
				canmove = true;		
			}			
		}
		
		// Neu la hang phia sau va khong cung mot cot
		else if( RectangleUtils.isNextRow( quanCao, quanThap ) 
						&& RectangleUtils.isColumnBeside( quanThap, quanCao ) ) {
			canmove = true;
		}
		
		// Dieu kien co ban khi di quan
		if ( canmove ) {
			canmove = isPassToBasicMoveCondition( board, destination, foundedPiece, listQuanCo );
		}

		return canmove;
	}
	
	/* (non-Javadoc)
	 * @see vn.nnn.shogi.quanco.QuanCo#upgrade(java.awt.Rectangle, java.util.ArrayList)
	 * Quan tuong vang khong the phong cap
	 */
	@Override
	public void upgrade( Rectangle destination, ArrayList<QuanCo> listQuanCo ) {		
		QuanTuongBacPhongCap tuongBacPhongCap = new QuanTuongBacPhongCap( destination, color, normalDirection, getMainApplet() );
		int idxChosen = getPositionInList( listQuanCo );
		
		listQuanCo.remove( idxChosen );
		listQuanCo.add( idxChosen, tuongBacPhongCap );
	}

	/* (non-Javadoc)
	 * @see vn.nnn.shogi.quanco.QuanCo#canRelease(vn.nnn.shogi.quanco.Board, java.awt.Rectangle, java.util.ArrayList)
	 */
	@Override
	public boolean canRelease( Board board, Rectangle destination,
			ArrayList<QuanCo> listQuanCo ) {
		
		return isPassToBasicReleaseCondition( board, destination, listQuanCo );
	}
}
